﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;
using NHunspell;

namespace CS2102_Music.Logic
{
    public class Utilities
    {
        //returns a mysql date string from a date
        //Str_To_Date('10/09/2013', '%d/%m/%Y')
        public static string ToSQLDateString(DateTime d)
        {
            string dateStr = d.Day.ToString() + "/" + d.Month.ToString() + "/" + d.Year.ToString();
            string timeStr = d.Hour.ToString() + ":" + d.Minute.ToString() + ":" + d.Second.ToString();
            return "Str_To_Date('" + dateStr + " " + timeStr + "', '%d/%m/%Y %H:%i:%s')";
        }

        //returns a proper ' ' enclosed sql string, with any special character ' escaped with the escape character \
        public static string ToSQLString(string s)
        {
            string str = s;
            string[] strs = str.Split('\'');
            str = "\'" + strs[0];
            for (int i = 1; i < strs.Length; i++)
            {
                str = str + "\\\'" + strs[i];// str = str + \' + strs[i]
            }
            str = str + "\'";
            return str;
        }

        //converts a date string in the format dd/mm/yyyy hh:mm into a DateTime
        public static DateTime ToDateTime(string s)
        {
            string str = s;
            string[] strs = str.Split('/');
            int day = Convert.ToInt32(strs[0]);
            int month = Convert.ToInt32(strs[1]);
            strs = strs[2].Split(' ');

            int year = Convert.ToInt32(strs[0]);

            strs = strs[1].Split(':');

            int hour = Convert.ToInt32(strs[0]);
            int minute = Convert.ToInt32(strs[1]);
            int second = Convert.ToInt32(strs[2]);

            DateTime d = new DateTime(year, month, day, hour, minute, second);
            return d;
        }

        public static string ToHtmlDateString(DateTime d)
        {
            string year = d.Year.ToString();
            string month = d.Month.ToString();
            string day = d.Day.ToString();

            month = month.PadLeft(2, '0');
            day = day.PadLeft(2, '0');

            return year + "-" + month + "-" + day;
        }

        //returns a string for the select field of a query to return a properly formatted date
        public static string DateFormatSQL(string name)
        {
            return "Date_format(" + name + ",'%d/%m/%Y %H:%i:%s')";
        }
        //enclose a date string in the format of dd/mm/yyyy in the appropriate mysql function to convert it into a mysql date
        public static string ToSQLDateString(string date)
        {
            return "Str_To_Date('" + date + "', '%d/%m/%Y %H:%i:%s')";
        }

        //returns dictionary of words similar to string input
        public static List<string> SuggestionsSpell(string string_in)
        {
            List<string> suggestions = new List<string>();
            using (Hunspell hunspell = new Hunspell("en_Us.aff", "en_Us.dic"))
            {
                suggestions = hunspell.Suggest(string_in);
            }

            return suggestions;
        }
    }
}
